package com.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.dao.GitDao;
import com.base.entity.GitEntity;
import com.base.entity.SysUserEntity;
import com.base.service.GitService;
import com.base.utils.GitUtils;
import com.base.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 功能描述:
 *
 * @Author: TianRay
 * @Date: 2021/3/16 9:49
 **/
@Service("gitService")
public class GitServiceImpl extends ServiceImpl<GitDao, GitEntity> implements GitService {

    @Override
    public Result saveGit() {
        GitUtils gitUtils = new GitUtils();
        try{
            // 获取所有的版本号与版本号对应的时间戳
            ArrayList<HashMap<String, Object>> gitVersions = gitUtils.getGitVersion();
            for (HashMap<String, Object> gitVersion : gitVersions) {
                if (BeanUtil.isNotEmpty(gitVersion.get("version"))) {
                    // 查询数据库获取目前库中最新的日志记录
                    List<GitEntity> list = this.list(Wrappers.<GitEntity>lambdaQuery().select(GitEntity::getDate).orderByDesc(GitEntity::getDate).last("limit 1"));
                    // 比较时间,以前保存的记录不在保存
                    if (DateUtil.compare(list.get(0).getDate(),Convert.toDate(gitVersion.get("commitDate"))) < 0 ) {
                        // 根据指定版本号获取版本号下面的详情记录
                        HashMap<String, Object> version = gitUtils.getCommitLogList(gitVersion.get("version").toString());
                        if (MapUtil.isNotEmpty(version)){
                            GitEntity gitEntity = new GitEntity();
                            // 判断截取日志信息
                            String message = version.get("message").toString();
                            String[] messages = message.split(" ");
                            if (message.startsWith("#")) {
                                switch (messages[0]){
                                    case "#fix" : gitEntity.setType("fix"); break;
                                    case "#update" : gitEntity.setType("update"); break;
                                    case "#add" : gitEntity.setType("add"); break;
                                    case "#remove" : gitEntity.setType("remove"); break;
                                    default: gitEntity.setType("none"); break;
                                }
                                gitEntity.setMsg(messages[1]);
                                gitEntity.setPerson(version.get("commitPerson").toString());
                                gitEntity.setDate(Convert.toDate(version.get("commitDate")));
                                SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
                                gitEntity.setCreateBy(user.getUserId().toString());
                                this.save(gitEntity);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
           return Result.failure(e.getMessage());
        }
        return Result.success();
    }

    @Override
    public Result queryPage() {
        List<GitEntity> list = this.list(Wrappers.<GitEntity>lambdaQuery().groupBy(GitEntity::getDate).groupBy(GitEntity::getType).orderByDesc(GitEntity::getDate));

        List<HashMap<String, Object>> result = new ArrayList<>();

        HashMap<String, Object> data = new HashMap<>();
        List<Object> gitList = new ArrayList<>();
        for (GitEntity gitEntity : list) {
            // 将相同日期的数据存到同一个list中
            String format = DateUtil.format(gitEntity.getDate(), "yyyy-MM-dd");
            if (!data.containsValue(format)){
                gitList = new ArrayList<>();
                data = new HashMap<>();
                gitList.add(gitEntity);
                data.put("list",gitList);
                data.put("date",format);
                result.add(data);
            } else {
                gitList.add(gitEntity);
            }

        }

        return Result.success(result);
    }
}
